require.config({
    baseUrl:"../modules",
    paths:{

    }
})
const tbody = document.querySelector(".table-tbody");
console.log(1);
const checkall = document.querySelector("#checkall");
const allnums = document.querySelector(".allnum");
const toPrice = document.querySelector("#toPrice");
require(["getGoods","renderCars","setCarsLocal","num","allflag","allnum","allPrice","selectDel"],function(getGoods,renderCars,setCarsLocal,num,allflag,allnum,allPrice,selectDel){
const goodsMsg = localStorage.getItem("goodsMsg") ? JSON.parse(localStorage.getItem("goodsMsg")) : [];
    getGoods(res=>{
        renderCars(res,goodsMsg,tbody);
    })
    tbody.onclick = function(eve){
        const e = eve || window.event;
        const target = e.target || e.srcElement;
        num("minus",goodsMsg,target,i=>{
            if(goodsMsg[i].num > 0){
                goodsMsg[i].num--;
                target.nextElementSibling.value = goodsMsg[i].num;
                allnums.innerHTML =allnum(goodsMsg);
                toPrice.innerHTML = allPrice(goodsMsg);
                target.parentNode.parentNode.nextElementSibling.children[0].innerHTML=goodsMsg[i].num*goodsMsg[i].price;
            }
        });
        num("add",goodsMsg,target,i=>{
                goodsMsg[i].num++;
                target.previousElementSibling.value = goodsMsg[i].num;
                allnums.innerHTML =allnum(goodsMsg);
                toPrice.innerHTML = allPrice(goodsMsg);
                target.parentNode.parentNode.nextElementSibling.children[0].innerHTML=goodsMsg[i].num*goodsMsg[i].price;
                console.log(8);
        });
        if(target.id == "delS"){
            const id = target.parentNode.getAttribute("index");
            target.parentNode.remove();
            setCarsLocal(goodsMsg,id,i=>{
                goodsMsg.splice(i,1)
            })
            allnums.innerHTML =allnum(goodsMsg);
            toPrice.innerHTML = allPrice(goodsMsg);
        }
    }
    tbody.oninput = function(eve){
        const e = eve || window.event;
        const target = e.target || e.srcElement;
        if(target.className == "checkone"){
            const id =target.parentNode.parentNode.getAttribute("index");
            setCarsLocal(goodsMsg,id,i=>{
                goodsMsg[i].flag = Number(target.checked);
            })
            checkall.checked = goodsMsg.every(val=>val.flag);
            allnums.innerHTML =allnum(goodsMsg);
            toPrice.innerHTML = allPrice(goodsMsg);
        }
    }
    checkall.checked = goodsMsg.every(val=>val.flag);
    checkall.oninput = function(){
        allflag(goodsMsg,checkall.checked);
        allnums.innerHTML =allnum(goodsMsg);
        toPrice.innerHTML = allPrice(goodsMsg);
    }
    allnums.innerHTML =allnum(goodsMsg);
    toPrice.innerHTML = allPrice(goodsMsg);
    alldel.onclick = function(){
        const check = document.querySelectorAll(".checkone");
        selectDel(goodsMsg,check);
        allnums.innerHTML =allnum(goodsMsg);
        toPrice.innerHTML = allPrice(goodsMsg);
        // console.log(toPrice.innerHTML );
    }
})